<!--
 * @Descripttion: 考试计划
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2021-05-17 20:19:20
 * @LastEditTime: 2021-05-19 11:40:52
 * @FilePath: \cscec-pms-admin\src\views\partyMasses\partyManage\examinationPlan\index.vue
-->
<template>
  <!-- 考题题库 -->
  <base-breadcrumb class="flexMemberInfo-container">
    <!-- 搜索 -->
    <searchData
      :search-data="searchOption"
      @search="search"
    />
    <!-- 搜索 -->
    <!-- 考试题库列表 -->
    <a-card>
      <!-- 表格操作 -->
      <div class="table-operator">
        <!-- <title-name :title="$route.meta.title+'列表'" /> -->
        <a-button
          v-btnPermission="'pa_011_add_01'"
          type="primary"
          icon="plus"
          @click="handleAdd"
        >
          新增
        </a-button>
      </div>
      <!-- 表格操作 -->
      <!-- 表格统计 -->
      <div class="pub-table-select-count">
        <a-icon
          type="info-circle"
          theme="twoTone"
        />
        <span style="margin-right: 12px">
          共计：<span class="totalNum">{{ total||0 }} </span>个
        </span>
      </div>
      <!-- 表格统计 -->
      <!-- 表格 -->
      <list-table
        ref="table"
        size="default"
        row-key="id"
        :columns="columns"
        :request="loadData"
        :alert="true"
        show-size-changer
        show-quick-jumper
      >
        <span
          slot="operator"
          slot-scope="text,record"
        >
          <MoreButton>
            <template v-if="record.state === 1 ">
              <a
                v-btnPermission="'pa_011_loo_04'"
                @click="handleDetail(record)"
              >查看</a>
            </template>
            <template v-if="record.state === 0 ">
              <a
                v-btnPermission="'pa_011_edi_02'"
                @click="handleEdit(record)"
              >编辑</a>
            </template>
            <template v-if="record.state === 1 ">
              <a
                v-btnPermission="'pa_011_exp_05'"
                :href="'/api'+unionApiUrl+'/examPlan/exportExamPlanMember?access_token='+token+'&examPlanId='+record.id"
                target="_parent"
              >导出</a>
            </template>
            <a
              v-if="record.state === 0 "
              v-btnPermission="'pa_011_del_03'"
              @click="handleDelete(record)"
            >删除</a>
          </MoreButton>
        </span>
      </list-table>
      <!-- 表格 -->
    </a-card>
    <!-- 考试题库列表 -->
  </base-breadcrumb>
</template>
<script>
import { STable } from '@/components'
import { unionApiUrl } from '@/api/config.js'
import { getPageExamPlan, deleteByPrimaryKey } from '@/api/partyMasses/partyManage/examinationPlan'
import Vue from 'vue'
export default {
  name:'a'+ Date.now(),
  // name: 'ExaminationPlan',
  components: {
    STable
  },
  data() {
    return {
      // 网关
      unionApiUrl,
      // 搜索组件配置
      searchOption: [
        {
          name: '考试计划名称',
          value: 'examPlanName',
          type: 'input'
        },
        {
          name: '考卷名称',
          value: 'examPaperName',
          type: 'input'
        },
        {
          name: '状态',
          value: 'examPlanState',
          type: 'select',
          data: [
            {
              value: 0,
              label: '未发送'
            },
            {
              value: 1,
              label: '已发送'
            }
          ]
        },
        {
          name: '计划开始时间',
          value: 'startEndDate',
          type: 'dateRange'
        }
      ],
      // 表头
      columns: [
        {
          title: '序号',
          width: '80px',
          align: 'center',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return index + 1
          }
        },
        {
          title: '考试计划名称',
          dataIndex: 'name',
          key: 'name',
          align: 'center'
        },
        {
          title: '考卷名称',
          dataIndex: 'examPaperName',
          key: 'examPaperName'
        },
        {
          title: '考卷总分',
          dataIndex: 'totalScore',
          key: 'totalScore',
          align: 'center',
          customRender: (text, record, index) => {
            return text ? text : 0
          }
        },
        {
          title: '考试人数',
          dataIndex: 'totalMembers',
          key: 'totalMembers',
          align: 'center',
          customRender: (text, record, index) => {
            return text ? text : 0
          }
        },
        {
          title: '已考试人数',
          dataIndex: 'referMembers',
          key: 'referMembers',
          align: 'center',
          customRender: (text, record, index) => {
            return text ? text : 0
          }
        },
        {
          title: '计划开始时间',
          dataIndex: 'startTime',
          key: 'startTime',
          align: 'center'
        },
        {
          title: '计划结束时间',
          dataIndex: 'endTime',
          key: 'endTime',
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTime',
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'stateName',
          key: 'stateName',
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operator',
          width: '170px',
          align: 'center',
          scopedSlots: { customRender: 'operator' }
        }
      ],
      // 查询参数
      searchData: {},
      // 表格共计
      total: 0,
      // token
      token: ''
    }
  },
  created() {
    this.token = Vue.getAcloudProvider().getAccessToken()
  },
  methods: {
    /**
     * @description: 搜索
     * @param {Object} value 搜索数据
     * @return {Void}
     */
    search(value) {
      this.searchData = value || {}
      this.$refs.table.refresh(true)
    },
    /**
     * @description: 表格数据加载方法
     * @param {Object} parameter 分页信息
     * @return {Promise} 接口Promise
     */
    async loadData(parameter) {
      try {
        const params = Object.assign({}, parameter, { query: this.searchData })
        if (this.searchData?.startEndDate?.length > 0) {
          params.query.startTime = this.searchData.startEndDate[0].format('YYYY-MM-DD') || void 0
          params.query.endTime = this.searchData.startEndDate[1].format('YYYY-MM-DD') || void 0
        }
        const { data, currentPage, totalSize } = await getPageExamPlan(params)
        this.total = totalSize
        return Promise.resolve({
          code:200,
          data,
          currentPage,
          totalSize
        })
      } catch (error) {
        console.log(error)
        return Promise.reject(error)
      }
    },
    /**
     * @description: 新增
     * @param {Void}
     * @return {Void}
     */
    handleAdd() {
      this.$router.push({
        path: '/partyMasses/partyManage/examinationPlan/update',
        query: {
          id: '',
          pageType: 'add'
        }
      })
    },
    /**
     * @description: 查看
     * @param {Void}
     * @return {Void}
     */
    handleDetail(record) {
      this.$router.push({
        path: '/partyMasses/partyManage/examinationPlan/detail',
        query: {
          id: record.id
        }
      })
    },
    /**
     * @description: 编辑
     * @param {Object} record 当前行数据
     * @return {Void}
     */
    handleEdit(record) {
      this.$router.push({
        path: '/partyMasses/partyManage/examinationPlan/update',
        query: {
          id: record.id,
          pageType: 'edit'
        }
      })
    },
    /**
     * @description: 删除
     * @param {Object} record 当前行数据
     * @return {Void}
     */
    handleDelete(record) {
      this.$confirm({
        title: '温馨提示',
        content: '确定是否删除数据？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk: () => {
          deleteByPrimaryKey({ id: record.id })
            .then(({ code }) => {
              if (code === this.SUCCESS_CODE) {
                this.$message.success('删除成功！')
                this.$refs.table.refresh(true)
              } else {
                this.$message.error('删除失败！')
              }
            })
            .catch((error) => {
              console.log(error)
              this.$message.error('删除失败！')
            })
        }
      })
    },
    /**
     * @description: 刷新列表
     * @param {Void}
     * @return {Void}
     */
    handleRefresh() {
      this.search()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
